package com.mmp.main.servlet.report;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mmp.common.servlet.BasicServlet;
import com.mmp.main.entity.HospitalDevicePpmDetail;
import com.mmp.main.util.Enums;
import com.mmp.main.util.WebUtil;

public class ViewPPMNotHappenedVisits extends BasicServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		int year = WebUtil.getParamValueAsInteger(request, "year", 0);
		int month = WebUtil.getParamValueAsInteger(request, "month", 0);
		int category = WebUtil.getParamValueAsInteger(request, "category", 0);

		Map<Integer, Object> map = new HashMap<Integer, Object>();
		int i = 0;
		String jpql = "select h from HospitalDevicePpmDetail h where h.visitStatus = " + Enums.PPM_VISIT_STATUS.NOT_HAPPENED.getId() + "   ";

		if (month != 0) {
			jpql += " and MONTH(h.visitDate) = ? ";
			map.put(++i, month);
		}

		if (month != 0) {
			jpql += " and YEAR(h.visitDate) = ? ";
			map.put(++i, year);
		}

		if (category != 0) {
			jpql += " and h.hospitalDevice.lookupDeviceCategory.id = ? ";
			map.put(++i, category);
		}

		jpql += " order by h.hospitalDevice.deviceName ";

		List<HospitalDevicePpmDetail> ppmList = dbm.queryJPQL(jpql, map, HospitalDevicePpmDetail.class);
		if (ppmList == null || ppmList.isEmpty()) {
			announceErorrMessage(request, response, "لم يتم إدخال معلومات الزيارات الوقائية للأجهزة المطلوبة في الزمن الذي تم تحديده");
		} else {
			request.setAttribute("ppmList", ppmList);
			viewHTMLReport(request, response, "report/ViewPPMNotHappenedAttachedFiles.jsp", Enums.REPORT_TITLE.NOT_HAPPENED_PPM_VISITS.getReportTitle());
		}
	}

	/*
	 * int month = WebUtil.getParamValueAsInteger(request, "month", 0); int
	 * category = WebUtil.getParamValueAsInteger(request, "category", 0);
	 * 
	 * Map<Integer, Object> map = new HashMap<Integer, Object>(); String jpql =
	 * "select h from HospitalDevicePpmDetail h where h.visitStatus = " +
	 * Enums.PPM_VISIT_STATUS.NOT_HAPPENED.getId() + " and h.visitMonth = ? ";
	 * map.put(1, month); if (category != 0) { jpql +=
	 * " and h.hospitalDevice.lookupDeviceCategory.id = ? "; map.put(2,
	 * category); }
	 * 
	 * jpql += " order by h.hospitalDevice.deviceName ";
	 * 
	 * HashMap params = new HashMap();
	 * params.put(Default.REPORT_TITLE_PARAM_NAME,
	 * Enums.REPORT_TITLE.NOT_HAPPENED_PPM_VISITS.getReportTitle());
	 * params.put("ppmList", dbm.queryJPQL(jpql, map,
	 * HospitalDevicePpmDetail.class)); viewPDFReport("r1.jasper", null, params,
	 * response);
	 */
}
